 //
//  SelectView.m
//  onlineeducation_ios
//
//  Created by teammac3 on 2017/6/29.
//  Copyright © 2017年 zxj. All rights reserved.
//

#import "SelectView.h"

@interface SelectView()<UITableViewDelegate,UITableViewDataSource>

@end
@implementation SelectView

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        
        UIView *bgView = [UIView new];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.6;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [bgView addGestureRecognizer:tap];
        [self addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.mas_top);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
    }
    return self;
}

- (void)setData:(NSMutableArray *)data{
    _data = data;
    
    
    UITableView *tableV = [UITableView new];
    tableV.delegate = self;
    tableV.dataSource = self;
    tableV.rowHeight = 54.5;
    tableV.scrollEnabled = NO;
    [self addSubview:tableV];
    [tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.top.mas_equalTo(self.mas_top).mas_offset(175);
//        make.centerX.mas_equalTo(self);
//        make.width.mas_equalTo(325);
//        make.height.mas_equalTo(_data.count*54.5);
        
        make.height.mas_equalTo(_data.count*54.5);
        make.bottom.equalTo(self.mas_bottom);
        make.left.offset(0);
        make.right.offset(0);
    }];
    
}

#pragma mark - 表格代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //文字
        UILabel *textL = [UILabel new];
        textL.textColor = [UIColor colorWithHexString:@"#333333"];
        textL.font = [UIFont systemFontOfSize:17];
        textL.text = _data[indexPath.row];
        textL.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:textL];
        [textL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.mas_equalTo(cell);
            make.left.mas_equalTo(cell).mas_offset(10);
            make.right.mas_equalTo(cell).mas_offset(-10);
            make.height.mas_equalTo(cell.mas_height);
        }];
        
//        ViewRadius(cell, 8);
//        ViewRadius(tableView, 8);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 选择设备号
    self.strBlock(_data[indexPath.row]);
    [self removeFromSuperview];
}

- (void)tapAction{
    
    [self removeFromSuperview];
}
@end
